-- open_grid
-- create by panyl
-- 技能: 地震术，开启 10 + 0.6 * 魔力 个格子

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 威力
        local force = para[1];

        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, true);
        --最终威力
        force = math.modf(force * (addon_rate + 1000)/1000);

        -- 2. 攻击方的魔力
        local magic = source:getMagic();

        -- 被对方百分比削弱
        local prop = PropM.combine(target, "weak_enemy", "magic");
        magic = PropM.apply(prop, magic);

        -- 被对方绝对值削弱
        prop = PropM.combine(target, "weak_enemy2", "magic");
        magic = PropM.apply(prop, magic);

        -- 3. 效果
        local num = math.modf(10 + force * magic /1000);
        num = math.min(30, num);

        -- 所有格子
        local grids = DungeonM.getCurrentDungeon();
        local i = 1;
        for _, grid in pairs(grids) do
            if i > num then
                break;
            end
            if not grid:isOpened() then
                grid:open();
                i = i + 1;
            end
        end

        -- 是否附带眩晕
        prop = PropM.combine(source, "skill_add_spin", skillId);
        local isSpin = PropM.apply(prop, 0);

        if isSpin ~= 0 then
            -- 眩晕,持续3回合,不包括本回合
            local propId = PropM.getPropId("spin");
            local effectRound = round + 4;
            -- 持续回合加成
            prop = PropM.combine(source, "magic_last", skillId);
            effectRound = PropM.apply(prop, effectRound);
            prop = PropM.combine(source, "magic_last", "assist");
            effectRound = PropM.apply(prop, effectRound);

            -- 所有技能持续时间+
            prop = PropM.combine(source, "magic_last", "all");
            effectRound = PropM.apply(prop, effectRound);

            local condition = {
                ["id"] = propId,
                ["end_round"] = effectRound,
            }

            local monster;
            -- 再遍历一次，对所有在场的怪物附加眩晕状态
            for _, grid in pairs(grids) do
                if grid:isOpened() and grid:isMonster() and not grid.monster:isDead() and not grid.monster:isBoss() then
                    CombatStatusM.applyStatus(grid.monster, "spin", condition);
                end
            end
        end

        -- 地震术没有光效但需有音效效果
        AudioM.playFx("skill_dizheng");

        -- 作用目标
        return grids;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 威力
        local base = para[1];
        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, false);
        -- 最终威力
        local force = math.modf(base * (addon_rate + 1000)/1000);

        -- 2. 攻击方的魔力
        local magic = source:getMagic();

        -- 3. 效果
        local open_grid_base = math.modf(10 + base * magic /1000);
        local open_grid_force = math.modf(10 + force * magic /1000);
        open_grid_base = math.min(30, open_grid_base);
        open_grid_force = math.min(30, open_grid_force);

        --local addon = open_grid_force - open_grid_base;
        desc = string.gsub(desc, "{open_grid}", open_grid_force);

        -- 如果是真义描述
        local key = FormulaM.invoke("CALC_SKILL_APPEND_PROP", skillId);
        local prop = PropM.combine(source, key, skillId);
        local isSpin = PropM.apply(prop, 0);
        if isSpin ~= 0 then
            -- 持续时间
            local effectRound = 3;
            -- 持续回合加成
            prop = PropM.combine(source, "magic_last", skillId);
            effectRound = PropM.apply(prop, effectRound);
            prop = PropM.combine(source, "magic_last", "assist");
            effectRound = PropM.apply(prop, effectRound);

            -- 所有技能持续时间+
            prop = PropM.combine(source, "magic_last", "all");
            effectRound = PropM.apply(prop, effectRound);
            desc = string.gsub(desc, "{round}", effectRound);
        end

        -- 系别TODO

        return desc;
    end,
};